package com.foreknow.view;

import com.foreknow.dao.BusinessDao;
import com.foreknow.dao.impl.BusinessDaoImpl;
import com.foreknow.model.Business;
import com.foreknow.model.BusinessTable;
import com.foreknow.service.impl.AdminServiceImpl;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Add extends JFrame {

	private JPanel contentPane;
	private JTextField bPassword;
	private JTextField bName;
	private JTextField bAddress;
	private JTextField bExplain;
	private JTextField starPrice;
	private JTextField deliveryPrice;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Add frame = new Add();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Add() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("密码：");
		lblNewLabel.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(28, 13, 68, 15);
		contentPane.add(lblNewLabel);
		
		JLabel label = new JLabel("商家名称：");
		label.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(10, 48, 86, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("商家地址：");
		label_1.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setBounds(10, 81, 86, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("商家介绍：");
		label_2.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(10, 116, 86, 15);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("起送费：");
		label_3.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(10, 148, 86, 15);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("配送费：");
		label_4.setFont(new Font("叶根友毛笔行书2.0版", Font.PLAIN, 15));
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setBounds(10, 183, 86, 15);
		contentPane.add(label_4);
		
		bPassword = new JTextField();
		bPassword.setBounds(106, 10, 275, 21);
		contentPane.add(bPassword);
		bPassword.setColumns(10);
		
		bName = new JTextField();
		bName.setColumns(10);
		bName.setBounds(106, 48, 275, 21);
		contentPane.add(bName);
		
		bAddress = new JTextField();
		bAddress.setColumns(10);
		bAddress.setBounds(106, 81, 275, 21);
		contentPane.add(bAddress);
		
		bExplain = new JTextField();
		bExplain.setColumns(10);
		bExplain.setBounds(106, 113, 275, 21);
		contentPane.add(bExplain);
		
		starPrice = new JTextField();
		starPrice.setColumns(10);
		starPrice.setBounds(106, 145, 275, 21);
		contentPane.add(starPrice);
		
		deliveryPrice = new JTextField();
		deliveryPrice.setColumns(10);
		deliveryPrice.setBounds(106, 180, 275, 21);
		contentPane.add(deliveryPrice);
		
		JButton button = new JButton("添加");
		button.setBounds(75, 229, 93, 23);
		contentPane.add(button);
		button.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				BusinessServiceImpl bService = new BusinessServiceImpl();
				Business business = new Business();
				business.setPassword(bPassword.getText());
				business.setBusinessName(bName.getText());
				business.setBusinessAddress(bAddress.getText());
				business.setBusinessExplain( bExplain.getText());
				business.setStarPrice(BigDecimal.valueOf(Float.valueOf(starPrice.getText())));
				business.setDeliveryPrice(BigDecimal.valueOf(Float.valueOf(deliveryPrice.getText())));
				bService.add(business);

				MainWindow mainWindow = new MainWindow();
				mainWindow.refresh();

				Add.this.dispose();
			}
		});
		
		JButton button_1 = new JButton("取消");
		button_1.setBounds(270, 229, 93, 23);
		contentPane.add(button_1);
		button_1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                MainWindow mainWindow = new MainWindow();
                Add.this.dispose();
            }
        });
		this.setVisible(true);
	}
}
